/*------------------------------------------------------------------------
//                          Project CommonLib                           //
------------------------------------------------------------------------*/

#include "stdafx.h"
#include "DbgTrace.h"

namespace Trace
{
	void DebugOutputSingle(LPCWSTR locationString, E_FLAG flag, TCHAR *format, ... )
	{
		static TCHAR buf[SIZE_DEBUG_TRACE + 1]={0,};
		CmVAR_ARGS_UNI( buf, CmCOUNTOF(buf), format);

		if(eTRACE_TIME==flag)
		{
			static TCHAR bufTime[SIZE_TRACE_TIME +SIZE_DEBUG_TRACE+ 1]={0,};
			static struct tm* curTime = NULL;

			curTime = CmTime::GetLocalTimes();
			if(NULL==curTime) 
			{				
				return;    
			}
			SPRINTF( bufTime, SIZE_TRACE_TIME, TEXT("[%02d:%02d:%02d] %s%s"), curTime->tm_hour, curTime->tm_min, curTime->tm_sec, locationString, buf);     
			::OutputDebugString( bufTime);
		}
		else
		{
			::OutputDebugString( buf);
		}
	}

	
	__declspec(selectany) CriticalObj g_TraceLock(FALSE);
	    
	void DebugOutput(LPCWSTR locationString, E_FLAG flag, TCHAR *format, ... )
    {
		g_TraceLock.Lock();
        
    
        TCHAR buf[SIZE_DEBUG_TRACE + 1]={0,};
        CmVAR_ARGS_UNI( buf, CmCOUNTOF(buf), format);

        if(eTRACE_TIME==flag)
        {
			TCHAR wTrace[SIZE_TRACE_TIME +SIZE_DEBUG_TRACE+ 1]={0,};
			TCHAR time[24];
			STRTIME( time, 24);
			TCHAR date[24];
			errno_t err = ::_wstrdate_s( date, 24 );
			if (err)
			{			
				SPRINTF(wTrace, SIZE_LOG_PRINT+SIZE_LOG_TIME-1, L"%s%s", locationString, buf );
			}			
			else
			{
				//::wnsprintf(wTrace, SIZE_LOG_PRINT+SIZE_LOG_TIME-1, TEXT("[%s %s]: %s"), date, time, buf );
				SPRINTF(wTrace, SIZE_LOG_PRINT+SIZE_LOG_TIME-1, TEXT("%s[%s %s]: %s"), locationString, date, time, buf );
			}

            ::OutputDebugString( wTrace);
        }
        else
        {
            ::OutputDebugString( buf);
        }

        g_TraceLock.Unlock();		
    }
}

/*------------------------------------------------------------------------

------------------------------------------------------------------------*/